import time,random
from common.login import Login
from common.report_generate import Report
from selenium.webdriver.support.select import Select

class Obtain:
    def __init__(self):
        self.st = Login()
        self.driver = self.st.driver
        self.insert = Report()
        #超级管理员登录
        self.st.login_fast('超级管理员')
        # #打开资源管理-培训资源模块

     #技术面试面试结果更新保存，显示
    def interview(self):
        self.st.open_module('就业管理', '技术面试')
        self.driver.find_element_by_xpath('//*[@id="stuInfo_table"]/tbody/tr/td[9]/button').click()
        time.sleep(1)
        # 面试结果随机下标
        select_type = Select(self.driver.find_element_by_xpath('//*[@id="isPassSkill"]'))
        select_options = select_type.options
        random_index = random.randint(0, len(select_options) - 1)
        select_type.select_by_index(random_index)
        text = select_options[random_index].text

        self.driver.find_element_by_xpath('//*[@id="squestion"]').send_keys('test')
        self.driver.find_element_by_xpath('//*[@id="sval"]').send_keys('test')

        self.driver.find_element_by_xpath('//*[@id="saveSkillbtn"]').click()
        time.sleep(1)

        try:
            self.driver.find_element_by_xpath('/html/body/div[9]/div/div/div[3]/button').click()
            time.sleep(1)
            self.driver.refresh()
            result = self.driver.find_element_by_xpath('//*[@id="stuInfo_table"]/tbody/tr/td[8]').text
            if result == '未面试' and text == '未通过':
                print('保存成功，面试结果更新成功')
                self.insert.write_report('3.5', '就业管理', 'GUI测试', 'TC-023', '面试结果更新', '成功', '无', '无')
            elif result == text:
                print('保存成功，面试结果更新成功')
                self.insert.write_report('3.5', '就业管理', 'GUI测试', 'TC-023', '面试结果更新', '成功', '无', '无')
            else:
                print('保存成功，面试结果更新失败')
                img = self.insert.pic_screenshot()
                self.insert.write_report('3.5', '就业管理', 'GUI测试', 'TC-023', '面试结果更新', '失败', '无', img)
            print('面试结果保存成功')
            self.insert.write_report('3.5', '就业管理', 'GUI测试', 'TC-024', '面试结果保存', '成功', '无', '无')
        except:
            print('面试结果保存失败')
            imge = self.insert.pic_screenshot()
            self.insert.write_report('3.5', '就业管理', 'GUI测试', 'TC-024', '面试结果保存', '失败', '无', imge)


    #就业管理更新面试，入职情况
    def turn_obtain(self):
        self.driver.find_element_by_link_text('就业管理').click()
        time.sleep(1)
        self.driver.find_element_by_xpath('//*[@id="list-5"]/div/ul/li[2]/a').click()
        time.sleep(1)

        text = self.driver.find_element_by_xpath('//*[@id="stuInfo_table1"]/tbody/tr[1]/td[1]').text
        print(f'text为：{text}')

        #点击面试
        self.driver.find_element_by_xpath('//*[@id="stuInfo_table1"]/tbody/tr[1]/td[10]/button[1]').click()
        time.sleep(1)
        self.driver.find_element_by_link_text('入职情况').click()
        time.sleep(1)
        self.driver.find_element_by_xpath('//*[@id="form-jobRegi"]/div[1]/div[1]/button').click()
        time.sleep(1)
        self.driver.find_element_by_xpath('//*[@id="form-jobRegi"]/div[1]/div[1]/div/div/input').send_keys('阿里巴巴')
        Select(self.driver.find_element_by_xpath('//*[@id="form-jobRegi"]/div[2]/select')).select_by_index(1)
        self.driver.find_element_by_id('jdate').click()
        time.sleep(1)
        self.driver.find_element_by_xpath('/html/body/div[9]/div[3]/table/tbody/tr[3]/td[7]').click()
        self.driver.find_element_by_id('jsalary').send_keys('15000')
        self.driver.find_element_by_id('jremark').send_keys('test')
        self.driver.find_element_by_id('saveEditJBtn').click()
        time.sleep(1)

        # 点击已入职勾选框
        self.driver.find_element_by_id('entry0').click()
        self.driver.find_element_by_id('protocol0').click()

        time.sleep(3)
        #点击提交
        self.driver.find_element_by_xpath('//*[@id="stuInfo_table1"]/tbody/tr[1]/td[10]/button[2]').click()
        time.sleep(1)
        alter_text = self.driver.switch_to_alert().text
        if alter_text == '提交修改成功.':
            self.driver.switch_to_alert().accept()
            print('提交更新成功')
            self.insert.write_report('3.5', '就业管理', 'GUI测试', 'TC-029', '入职信息更新', '成功', '无', '无')
            time.sleep(1)

            #到入职信息模块确认
            self.driver.find_element_by_link_text('入职信息').click()
            time.sleep(1)

            try:
                result = self.driver.find_element_by_xpath('//*[@id="stuInfo_table"]/tbody/tr/td[1]').text
                print(f'result为:{result}')
                if result == text:
                    print('成功查询到就业信息')
                    self.insert.write_report('3.5', '就业管理', 'GUI测试', 'TC-030', '入职信息添加', '成功', '无', '无')
                else:
                    print('更新就业信息失败')
                    img = self.insert.pic_screenshot()
                    self.insert.write_report('3.5', '就业管理', 'GUI测试', 'TC-030', '入职信息添加', '成功', '无', img)
            except:
                print('更新就业信息失败')
                img = self.insert.pic_screenshot()
                self.insert.write_report('3.5', '就业管理', 'GUI测试', 'TC-030', '入职信息添加', '成功', '无', img)

        else:
            print('提交更新失败')
            img = self.insert.pic_screenshot()
            self.insert.write_report('3.5', '就业管理', 'GUI测试', 'TC-029', '入职信息更新', '成功', '无', img)

    #入职信息模块修改
    def obtain_update(self):

        self.st.open_module('就业管理', '入职信息')
        self.driver.find_element_by_xpath('//*[@id="stuInfo_table"]/tbody/tr[1]/td[9]/button').click()
        time.sleep(1)
        select_type = Select(self.driver.find_element_by_xpath('//*[@id="form-jobRegi"]/div[1]/select'))
        select_options = select_type.options
        random_index = random.randint(0, len(select_options) - 1)
        select_type.select_by_index(random_index)
        #text = select_options[random_index].text
        self.driver.find_element_by_id('jdate').click()
        time.sleep(1)
        self.driver.find_element_by_xpath('/html/body/div[8]/div[3]/table/tbody/tr[3]/td[3]').click()
        self.driver.find_element_by_id('saveEditJBtn').click()
        time.sleep(1)

        result = self.driver.find_element_by_xpath('/html/body/div[10]/div/div/div[2]/div').text
        if result == '修改成功！':
            print('修改成功')
        else:
            print('修改失败')

    # def caiwu(self):
    #     self.st.open_module('财务管理', '账户管理')
    #     self.driver.find_element_by_xpath('//*[@id="accountInfo"]/div[1]/div/div/button').click()





if __name__ == '__main__':
    Obtain().obtain_update()


